<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MSecret
{

    
    public static function passSign($password, $salt){
        $md5password =  md5(md5($password).$salt);
        return $md5password;
    }

    
    public static function encryptHex($key, $text){
        $crypttext = self::idtagDesEncode($key, $text);
        $cryptHex = self::strToHex($crypttext);
        return $cryptHex;
    }


    
    public static function decryptHex($key, $text){
        $crypttext = self::idtagDesDecode($key, self::hexToStr($text));
        return $crypttext;
    }


    
    public static function strToHex($string)
    {
        $hex="";
        for   ($i=0;$i<strlen($string);$i++)
        $hex.=dechex(ord($string[$i]));
        $hex=strtoupper($hex);
        return $hex;
    }


    
    public static function hexToStr($hex)
    {
        $string="";
        for   ($i=0;$i<strlen($hex)-1;$i+=2)
        $string.=chr(hexdec($hex[$i].$hex[$i+1]));
        return $string;
    }

    public static function idtagDesDecode($key,$encrypted)
    {
        $encrypted = base64_decode($encrypted);

        $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,'');         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        $ks = mcrypt_enc_get_key_size($td);
        mcrypt_generic_init($td, $key, $key);       
        $decrypted = mdecrypt_generic($td, $encrypted);       
        mcrypt_generic_deinit($td);               mcrypt_module_close($td);

        $y=self::pkcs5_unpad($decrypted);
        return $y;
    }

    public static function idtagDesEncode($key,$text)
    {
        $y=self::pkcs5_pad($text);

        $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,'');                 $ks = mcrypt_enc_get_key_size($td);
        mcrypt_generic_init($td, $key, $key);       
        $encrypted = mcrypt_generic($td, $y);       
        mcrypt_generic_deinit($td);               mcrypt_module_close($td);

        return base64_encode($encrypted);
    }

    public static function pkcs5_pad($text,$block=8)
    {
        $pad = $block - (strlen($text) % $block);
        return $text . str_repeat(chr($pad), $pad);
    }


    public static function pkcs5_unpad($text)
    {
        $pad = ord($text{strlen($text)-1});
        if ($pad > strlen($text)) return $text;
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return $text;
        return substr($text, 0, -1 * $pad);
    }
}
?>